﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Tools.Model
{
    /// <summary>
    /// 业务执行结果
    /// </summary>
    public class ExcutedResult
    {
        Dictionary<string, object> Data = new Dictionary<string, object>();
        /// <summary>
        /// 添加项，存在时不重复添加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public ExcutedResult AddItem(string key, object value)
        {
            if (!Data.ContainsKey(key)) Data.Add(key, value);
            return this;
        }
        /// <summary>
        /// 清除所有项
        /// </summary>
        /// <returns></returns>
        public ExcutedResult Clear()
        {
            Data.Clear();
            return this;
        }
        /// <summary>
        /// 设置项数据
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public ExcutedResult SetItems(Dictionary<string, object> items)
        {
            Data = items ?? new Dictionary<string, object>();
            return this;
        }

        /// <summary>
        /// 添加项，存在时更新数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public ExcutedResult Insert(string key, object value)
        {
            if (Data.ContainsKey(key)) Data.Remove(key);
            Data.Add(key, value);
            return this;
        }

        /// <summary>
        /// 查询是否存在Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool HasKey(string key)
        {
            return Data.ContainsKey(key);
        }

        public object GetObject(string key)
        {
            if (Data == null) return null;
            Data.TryGetValue(key, out object o);
            return o;
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetValue<T>(string key)
        {
            if (Data == null || !Data.ContainsKey(key))
            {
                return default;
            }
            object o = Data[key];
            if (o == null)
            {
                return default;
            }
            Type t = typeof(T);
            if (typeof(T).IsEnum)
            {
                return (T)Enum.Parse(t, o.ToString());
            }
            else
            {
                if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                {
                    NullableConverter nullableConverter = new NullableConverter(t);
                    t = nullableConverter.UnderlyingType;
                }
                return (T)Convert.ChangeType(o, t);
            }
        }

        /// <summary>
        /// 结果状态
        /// </summary>
        public ResultState State { get; set; }
        /// <summary>
        /// 错误代码
        /// </summary>
        public string Code { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 传回数据
        /// </summary>
        /// <summary>
        /// 异常
        /// </summary>
        public Exception Exception { get; set; }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static ExcutedResult Success(string code = null, string message = null, Dictionary<string, object> items = null)
        {
            var result = new ExcutedResult() { State = ResultState.Success, Code = code, Message = message };
            if (items != null)
            {
                result.SetItems(items);
            }
            return result;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ExcutedResult Failure(string message)
        {
            return new ExcutedResult() { State = ResultState.Failure, Code = null, Message = message };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ExcutedResult Failure(string code = null, string message = null)
        {
            return new ExcutedResult() { State = ResultState.Failure, Code = code, Message = message };
        }

        /// <summary>
        /// 失败 特殊Code
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ExcutedResult FailureSpecialCode(string message)
        {
            return new ExcutedResult() { State = ResultState.Failure, Code = "9999", Message = message };
        }

        /// <summary>
        /// 发生错误
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static ExcutedResult Error(Exception ex, string code = null, string message = null)
        {
            var result = new ExcutedResult() { State = ResultState.Error, Exception = ex, Code = code, Message = message };
            if (ex != null && string.IsNullOrWhiteSpace(message))
            {
                result.Message = ex.Message;
            }
            return result;
        }
    }

    /// <summary>
    /// 结果状态
    /// </summary>
    public enum ResultState
    {
        /// <summary>
        /// 操作成功
        /// </summary>
        Success = 0,
        /// <summary>
        /// 失败
        /// </summary>
        Failure = 1,
        /// <summary>
        /// 发生错误
        /// </summary>
        Error = 2
    }
}
